<?php

namespace App\Admin\Extensions;

use App\Model\User;
use App\Model\UserFreezeLog;
use App\Model\UserProfile;
use Encore\Admin\Admin;

class UserOp extends BaseAction
{
    protected function render()
    {
        $options = '';
        foreach (UserFreezeLog::REASON_TYPE as $k => $type) $options .= '<option value="' . $k . '">' . $type . '</option>';
        $html = '类型：<select id="freeze_reason_type" class="form-control input-sm" style="width:200px; margin:2px 15%">' . $options . '</select><br/>理由：<input id="freeze_reason" class="form-control input-sm" style="width:200px; margin:2px 15%"></input>';
        Admin::script($this->script($this->actionUrl, $html));
        $freezebtn = $this->buildButton('fail', '冻结设备+账号', ['id' => $this->id, 'status' => User::STATUS_FREEZE, 'action' => 'status', 'show-html' => 1, 'extra' => 'device']);


        $freezeAccountbtn = $this->buildButton('fail', '冻结账号', ['id' => $this->id, 'status' => User::STATUS_FREEZE, 'action' => 'status', 'show-html' => 1]);
        $okbtn = $this->buildButton('ok', '解除冻结', ['id' => $this->id, 'status' => User::STATUS_OK, 'action' => 'status']);
        $hostbtn = $this->buildButton('ok', '成为主播', ['id' => $this->id, 'status' => UserProfile::ROLE_HOST, 'action' => 'role']);
        $cancelBtn = $this->buildButton('fail', '注销', ['id' => $this->id, 'status' => User::STATUS_CANCELLATTED, 'action' => 'status']);
        $forbidBtn = $this->buildButton('fail', '禁言', ['id' => $this->id, 'status' => User::STATUS_FORBID, 'action' => 'status']);
        $forbid1D = $this->buildButton('fail', '禁言1天', ['id' => $this->id, 'status' => User::STATUS_FORBID, 'action' => 'status', 'extra' => 1440, 'show-html' => 1]);
        $forbid3D = $this->buildButton('fail', '禁言3天', ['id' => $this->id, 'status' => User::STATUS_FORBID, 'action' => 'status', 'extra' => 4320, 'show-html' => 1]);
        $forbid7D = $this->buildButton('fail', '禁言7天', ['id' => $this->id, 'status' => User::STATUS_FORBID, 'action' => 'status', 'extra' => 10080, 'show-html' => 1]);
        $forbid14D = $this->buildButton('fail', '禁言14天', ['id' => $this->id, 'status' => User::STATUS_FORBID, 'action' => 'status', 'extra' => 20160, 'show-html' => 1]);
        $cancelForbidBtn = $this->buildButton('ok', '取消禁言', ['id' => $this->id, 'status' => User::STATUS_OK, 'action' => 'status']);
        $realAvatarBtn = $this->buildButton('ok', '真人认证', ['id' => $this->id, 'status' => 1, 'action' => 'real_avatar']);
        $clearbtn = $this->buildButton('ok', '设置默认头像', ['id' => $this->id, 'status' => 1,'action' => 'default_avatar']);
        //冻结IP
        $freezebtnIp = $this->buildButton('fail', '冻结设备+账号+IP', ['id' => $this->id, 'extra' => $this->row->client_ip, 'status' => User::STATUS_FREEZE,'show-html' => 1, 'action' => 'freeze_ip_device']);
        $freezeIp = $this->buildButton('fail','冻结该IP', ['id' => $this->id, 'extra' => $this->row->client_ip, 'status' => User::STATUS_FREEZE, 'action' => 'freeze_ip']);
        switch ($this->row->status) {
                //正常
            case User::STATUS_OK:
                $str = [$freezebtn,$freezebtnIp, $freezeAccountbtn, $cancelBtn, $forbid1D, $forbid3D, $forbid7D, $forbid14D, $freezeIp,$clearbtn];
                break;
                //冻结
            case User::STATUS_FREEZE:
                $str = [$okbtn, $cancelBtn];
                break;
                //禁言
            case User::STATUS_FORBID:
                $str = [$freezebtn, $freezeAccountbtn, $cancelBtn, $cancelForbidBtn,$clearbtn];
                break;
            default:
                $str = [$freezebtn,$freezebtnIp, $freezeIp,$clearbtn];
                break;
        }

        if ($this->row->status != 2) {
            $recOkbtn = $this->buildButton('ok', '推荐', ['id' => $this->id, 'status' => 1, 'action' => 'rec']);
            $recbtn = $this->buildButton('ok', '正常展示', ['id' => $this->id, 'status' => 0, 'action' => 'rec']);
            $unrecbtn = $this->buildButton('fail', '不推荐', ['id' => $this->id, 'status' => -1, 'action' => 'rec']);
            //不推荐
            if ($this->row->userProfile) {
                //推荐
                if ($this->row->userProfile->is_recommend == 1) {
                    $str[] = $recbtn;
                    $str[] = $unrecbtn;
                } elseif ($this->row->userProfile->is_recommend == 0) {
                    $str[] = $recOkbtn;
                    $str[] = $unrecbtn;
                } else {
                    $str[] = $recOkbtn;
                    $str[] = $recbtn;
                }
            }
            if (
                $this->row->userProfile &&
                $this->row->userProfile->role == UserProfile::ROLE_USER &&
                $this->row->userProfile->gender == 0 &&
                $this->row->status == User::STATUS_OK
            ) {
                $str[] = $hostbtn;
            }
            if (
                $this->row->userProfile &&
                $this->row->userProfile->avatar_vrfy == 0
            ) {
                $str[] = $realAvatarBtn;
            }
        }

        return '<br/><br/>' . implode('<br/><br/>', $str);
    }
}
